package com.chuantou.yuba.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.FishingShopTO;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.UiUtil;
import com.chuantou.yuba.util.ViewHolder;

public class FishingShopAdapter extends BaseAdapter {
	private Context mContext;
	private boolean mShowDistance = true;
	private List<FishingShopTO> mShops = new ArrayList<FishingShopTO>();
	private boolean mIsPublic = true;

	public FishingShopAdapter(Context context) {
		mContext = context;
	}

	public void setShowDistance(boolean show) {
		mShowDistance = show;
	}

	public void setPublic(boolean isPublic) {
		mIsPublic = isPublic;
	}

	@Override
	public int getCount() {
		return mShops.size();
	}

	public void add(List<FishingShopTO> shops) {
		if (shops != null && !shops.isEmpty()) {
			mShops.addAll(shops);
		}
	}

	public void clear() {
		mShops.clear();
	}

	@Override
	public Object getItem(int position) {
		return mShops.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.fishing_shop_list_item, null);
		}
		FishingShopTO shopTO = mShops.get(position);
		NetworkImageView coverView = ViewHolder.get(convertView, R.id.image);
		BitmapBinder.bindNetImage(mContext, coverView, shopTO.getCover(), R.drawable.default_image_small, false);
		TextView titleView = ViewHolder.get(convertView, R.id.title);
		titleView.setText(shopTO.getName());
		TextView contentView = ViewHolder.get(convertView, R.id.content);
		contentView.setText(shopTO.getContent());
		TextView distanceView = ViewHolder.get(convertView, R.id.distance);
		View layout = ViewHolder.get(convertView, R.id.public_items);
		if (mIsPublic) {
			contentView.setPadding(0, 0, 0, 0);
			layout.setVisibility(View.VISIBLE);
			contentView.setMaxLines(1);
			contentView.setSingleLine(true);
			FrameLayout naviView = ViewHolder.get(convertView, R.id.fishing_navi);
			naviView.setOnClickListener(mNaviListener);
			naviView.setTag(shopTO);
			FrameLayout callView = ViewHolder.get(convertView, R.id.fishing_call);
			callView.setOnClickListener(mCallListener);
			callView.setTag(shopTO);
		} else {
			contentView.setMaxLines(2);
			contentView.setSingleLine(false);
			contentView.setPadding(0, mContext.getResources().getDimensionPixelSize(R.dimen.fishing_top_padding), 0, 0);
			layout.setVisibility(View.GONE);
		}
		if (shopTO.getTypeId() == FishingShopTO.TYPE_JINGXIAOSHANG && mShowDistance) {
			distanceView.setVisibility(View.VISIBLE);
			distanceView.setText(mContext.getString(R.string.distance_format, formatDistance(shopTO.getDistance())));
		} else {
			distanceView.setVisibility(View.GONE);
		}
		return convertView;
	}

	private OnClickListener mNaviListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Object tag = v.getTag();
			if (tag != null && tag instanceof FishingShopTO) {
				FishingShopTO shopTO = (FishingShopTO) tag;
				String location = shopTO.getLocation();
				String lat = "0";
				String lng = "0";
				if (!TextUtils.isEmpty(location) && location.contains(",")) {
					String[] array = location.split(",");
					lat = array[1];
					lng = array[0];
				}
				String point = lat + "," + lng;
				String label = shopTO.getAddress();
				String uriBegin = "geo:" + point;
				String query = point + "(" + label + ")";
				String encodedQuery = Uri.encode(query);
				String uriString = uriBegin + "?q=" + encodedQuery;
				Uri uri = Uri.parse(uriString);
				Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				if (UiUtil.isIntentAvailable(mContext, intent)) {
					mContext.startActivity(intent);
				} else {
					Toast.makeText(mContext, R.string.no_navi_app_installed, Toast.LENGTH_SHORT).show();
				}
			}
		}
	};

	private OnClickListener mCallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Object tag = v.getTag();
			if (tag != null && tag instanceof FishingShopTO) {
				FishingShopTO shopTO = (FishingShopTO) tag;
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setData(Uri.parse("tel:" + shopTO.getPhone()));
				mContext.startActivity(intent);
			}

		}
	};

	protected String formatDistance(double distance) {
		DecimalFormat format = new DecimalFormat();
		format.setMaximumFractionDigits(2);
		format.setMinimumFractionDigits(0);
		return format.format(distance);
	}

}